//
//  B_OrderDerailVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/30.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_OrderDerailVC.h"
#import "B_OrderHeaderView.h"
#import "B_OrderDetailTVC.h"
#import "B_OrderDetailLabelTVC.h"
#import "B_OrrderFooterView.h"
#import "B_TwoButtonRightFooterView.h"
#import "B_UserOrderDetailModel.h"

@interface B_OrderDerailVC ()

@property (nonatomic, strong) BaseView * mainView;

@property (nonatomic, strong) B_TwoButtonRightFooterView * footerView;

@property (nonatomic, strong) B_UserOrderDetailModel * orderDetailModel;

@end

@implementation B_OrderDerailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"订单详情"];
    self.view = self.mainView;
    [self getData];
}


/*
 需要根据不同的状态 分别配套底部的按钮状态
 */
- (BaseView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseView alloc] init];
        [_mainView addSubview:self.footerView];
        [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mainView.mas_left);
            make.right.equalTo(self.mainView.mas_right);
            make.bottom.equalTo(self.mainView.mas_bottom);
            make.height.equalTo(@(50));
        }];
        CGFloat bottom = SCREEN_HEIGHT > 800 ? (50 + 24) : 50;
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:-bottom];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_OrderDetailTVC" bundle:nil] forCellReuseIdentifier:@"B_OrderDetailTVC"];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_OrderDetailLabelTVC" bundle:nil] forCellReuseIdentifier:@"B_OrderDetailLabelTVC"];
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 2;
        };
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 1;
            } else {
                return 1;
            }
        };
        
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                B_OrderHeaderView * header = [B_OrderHeaderView initB_OrderHeaderView];
                header.B_orderNumber.text = [NSString stringWithFormat:@"订单号: %@", weSelf.orderDetailModel.orderNumber];
                header.B_orderStatus.text = [weSelf.orderDetailModel getUserOrderType:weSelf.orderDetailModel.type];
                header.B_userName.text = weSelf.orderDetailModel.deliveryName;
                header.B_userPhone.text = weSelf.orderDetailModel.deliveryPhone;
                header.B_userAddress.text = weSelf.orderDetailModel.address;
                header.B_merchantTitle.text = weSelf.orderDetailModel.nickName;
                return header;
            } else {
                return nil;
            }
        };
        _mainView.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                B_OrrderFooterView * footer = [B_OrrderFooterView initB_OrrderFooterView];
                footer.B_titleLabel.text = [NSString stringWithFormat:@"共计%@件, 共¥%.2f", weSelf.orderDetailModel.goodsNum, [weSelf.orderDetailModel.goodsPrice floatValue] * [weSelf.orderDetailModel.goodsNum intValue]];
                return footer;
            } else {
                return nil;
            }
        };
        
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.section == 0) {
                B_OrderDetailTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_OrderDetailTVC" forIndexPath:indexPath];
                cell.B_goodsImageView.backgroundColor = [UIColor redColor];
                cell.B_goodsTitle.text = weSelf.orderDetailModel.goodsName;
                cell.B_goodsPrice.attributedText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@", weSelf.orderDetailModel.goodsPrice]];
                cell.B_goodsCount.text = [NSString stringWithFormat:@"x %@", weSelf.orderDetailModel.goodsNum];
                return cell;
            } else {
                B_OrderDetailLabelTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_OrderDetailLabelTVC" forIndexPath:indexPath];
                cell.B_titleLabel.text = [NSString stringWithFormat:@"运单编号: %@", safeValue0(weSelf.orderDetailModel.waybillNumber)];;
                return cell;
            }
        };
    }
    return _mainView;
}


- (B_TwoButtonRightFooterView *)footerView {
    
    if (!_footerView) {
        WeakSelf(weSelf, self);
        _footerView = [B_TwoButtonRightFooterView initB_TwoButtonRightFooterView];
        //待付款状态  显示两个按钮  取消订单  立即付款
        if ([self.orderDetailModel.type isEqualToString:@"1"]) {
            [_footerView.B_leftButton setTitle:@"取消订单" forState:UIControlStateNormal];
            [_footerView.B_rightButton setTitle:@"立即付款" forState:UIControlStateNormal];
            _footerView.B_twoButtonAction = ^(UIButton *btn) {
                if ([btn.titleLabel.text isEqualToString:@"取消订单"]) {
                    /// TODO:取消订单
                } else {
                    /// TODO:立即付款
                }
            };
        } else if ([self.orderDetailModel.type isEqualToString:@"2"]) {
            _footerView.B_leftButton.hidden = YES;
            [_footerView.B_rightButton setTitle:@"确认收货" forState:UIControlStateNormal];
            _footerView.B_twoButtonAction = ^(UIButton *btn) {
                if ([btn.titleLabel.text isEqualToString:@"确认收货"]) {
                    /// TODO:确认收货
                } else {
                    
                }
            };
        } else if ([self.orderDetailModel.type isEqualToString:@"3"]) {
            _footerView.B_leftButton.hidden = YES;
            [_footerView.B_rightButton setTitle:@"再来一单" forState:UIControlStateNormal];
            _footerView.B_twoButtonAction = ^(UIButton *btn) {
                if ([btn.titleLabel.text isEqualToString:@"再来一单"]) {
                    /// TODO:再来一单
                } else {
                }
            };
        }
        
        //待收货状态  显示一个按钮  确认收货
        
        //已完成状态 A  显示一个按钮  再来一单  B 没有状态
        
        // 配型中状态  没有
        
        //待发货状态显示一个按钮  发货
    }
    return _footerView;
}


- (void) getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/order/selectOrderInfo/%@", self.B_OrderId] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            weSelf.orderDetailModel = [B_UserOrderDetailModel mj_objectWithKeyValues:data[@"values"]];
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
